//
//  ChoosePlayViewController.m
//  PlayerWorld
//
//  Created by 汪的一声就哭了！ on 16/6/22.
//  Copyright © 2016年 刘成. All rights reserved.
//
#import "TextCodeViewController.h"
#import "ChoosePlayViewController.h"
#import "UIKit+AFNetworking.h"
#import "G_P_Object.h"
#import "ByeCodeViewController.h"
#import "PLFiveObject.h"
@interface ChoosePlayViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation ChoosePlayViewController
{
    NSMutableArray *_listArr;
    PLFiveObject *_model;
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Game" forKey:@"Model"];
    [parametersDic setObject:@"GetTheIssueAndTime" forKey:@"Action"];
    [parametersDic setObject:self.G_N_Id forKey:@"Id"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _model = [[PLFiveObject alloc] init];
        [_model setValuesForKeysWithDictionary:dic];
//        NSLog(@"%@",dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
-(void)obtainG_PData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Game_Play" forKey:@"models"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _listArr = [NSMutableArray array];
        for (NSDictionary *dictionary in [dic objectForKey:@"HX_Game_Play"]) {
            G_P_Object *model = [[G_P_Object alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            if ([model.G_P_NameId isEqualToString:self.G_N_Id]) {
                if ([self.name isEqualToString:@"北京赛车"] && [model.G_P_Name isEqualToString:@"常规录入"]) {
                    if ([[[model.G_P_NumberName componentsSeparatedByString:@"|"] firstObject] length] == 1) {
                        model.G_P_Name = @"猜冠军常规录入";
                    }
                    if ([[[model.G_P_NumberName componentsSeparatedByString:@"|"] firstObject] length] == 2) {
                        model.G_P_Name = @"猜前二常规录入";
                    }
                    if ([[[model.G_P_NumberName componentsSeparatedByString:@"|"] firstObject] length] == 3) {
                        model.G_P_Name = @"猜前三常规录入";
                    }
                }
                else if ([self.name isEqualToString:@"北京赛车"] && [model.G_P_Name isEqualToString:@"手工录入"]) {
                    if ([[[model.G_P_NumberName componentsSeparatedByString:@"|"] firstObject] length] == 2) {
                        model.G_P_Name = @"猜前二手工录入";
                    }
                    if ([[[model.G_P_NumberName componentsSeparatedByString:@"|"] firstObject] length] == 3) {
                        model.G_P_Name = @"猜前三手工录入";
                    }
                }
                [_listArr addObject:model];
            }
        }
        [self.myTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.name;
    [self obtainData];
    [self obtainG_PData];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tab"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tab"];
    }
    G_P_Object *model = [_listArr objectAtIndex:indexPath.row];
    cell.textLabel.text = model.G_P_Name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    G_P_Object *model = [_listArr objectAtIndex:indexPath.row];
    if ([model.G_P_NumberName containsString:@"="]) {
        ByeCodeViewController *view = [[ByeCodeViewController alloc] init];
//        view.G_P_NumberName = model.G_P_NumberName;
        view.name = self.name;
//        view.G_P_Name = model.G_P_Name;
        view.model = _model;
        view.G_N_Id = self.G_N_Id;
        view.G_P_Model = model;
        [self.navigationController pushViewController:view animated:YES];
    }
    else{
        TextCodeViewController *view = [[TextCodeViewController alloc] init];
        view.G_P_Model = model;
        [view setHidesBottomBarWhenPushed:YES];
        view.model = _model;
        view.G_N_Id = self.G_N_Id;
        [self.navigationController pushViewController:view animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
